package com.wisezone.studentmanger.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import com.wisezone.studentmanger.model.Student;
import com.wisezone.studentmanger.util.DBConnection;

public class StudentDaoImpl extends DBConnection implements StudentDao {

	// 添加学生到数据库
	@Override
	public void addStudent(Student student) {
	}

	@Override
	public void deleteStudent(Student student) {

	}

	@Override
	public Student findStudentByNumber(int number) {
		return null;
	}

	@Override
	public List<Student> showStudentAll() {
		// 3.操作数据
		String sql = "SELECT * FROM student";
		Connection conn = getConnection();
		PreparedStatement ps = null;
		ResultSet rs = null;

		List<Student> studentLists = new ArrayList<>();
		try {
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			while (rs.next()) {
				int number = rs.getInt("number");
				String name = rs.getString("name");
				Timestamp time = rs.getTimestamp("datetime");

				Student student = new Student(name,number,time);
				System.out.println("学号:" + number + " 姓名:" + name + " 时间:" + time);
				studentLists.add(student);
			}

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			closeConnection(rs, ps, conn);
		}

		return studentLists;
	}
}
